package servlet;

import factory.ActionFactory;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

@WebServlet("/ActionServlet")
public class ActionServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        Properties props=new Properties();
        InputStream is= ActionServlet.class.getClassLoader().getResourceAsStream("function.properties");
        props.load(is);
        String op = props.getProperty(request.getParameter("op"));

        try {
            // 根据op获取类名
            Class action=Class.forName(op);
            // 创建对象
            ActionFactory actionFactory= (ActionFactory) action.newInstance();
            actionFactory.getAction(request,response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
